import { defineStore } from 'pinia'
import { getConfig, saveConfig } from '@/services/config.service'

export const useConfigStore = defineStore('config', {
  state: (): { data: Config } => ({
    data: {
      base: {
        logo: '',
        title: '',
        tel: '',
        qq: ''
      },
      login: {
        failNumber: 0,
        failLimitTime: 0
      }
    }
  }),
  actions: {
    async initData () {
      const config = await getConfig()
      if (config) {
        for (const k in config) {
          if ((config as any)[k]) {
            (this.data as any)[k] = (config as any)[k]
          }
        }
      }
    },
    async saveData (data: Config) {
      await saveConfig({
        base: this.data.base,
        login: this.data.login,
        ...data
      })
      this.data = { ...this.data, ...data }
    }
  }
})
